package com.sprucetec.live.controller;

import com.sprucetec.live.common.dto.LiveSystemLogDto;
import com.sprucetec.live.common.dto.PagedListDTO;
import com.sprucetec.live.common.dto.RedisCacheDto;
import com.sprucetec.live.common.dto.business.LiveChatDto;
import com.sprucetec.live.common.util.DateFormatUtils;
import com.sprucetec.live.common.util.RespCode;
import com.sprucetec.live.common.util.ServiceResult;
import com.sprucetec.live.common.util.ServiceResultUtil;
import com.sprucetec.live.entity.LiveSystemLog;
import com.sprucetec.live.service.LiveSystemLogService;
import com.sprucetec.live.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.ServerRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @ClassName LiveCacheManageController
 * @Description: 系统管理
 * @Author Kenny
 * @Date 2020/4/15
 **/
@Slf4j
@RestController
@RequestMapping("/liveSystem")
public class LiveSystemController {


    @Autowired
    private RedisService redisService;

    @Autowired
    private LiveSystemLogService liveSystemLogService;

    /**
     * @param dto
     * @return ServiceResult<PagedListDTO < PurchaseLog>>
     * @Description 日志查询
     */
    @RequestMapping(value = "/getLiveSystemLogPage")
    @ResponseBody
    public ServiceResult<PagedListDTO<LiveSystemLog>> getLiveSystemLogPage(@RequestBody LiveSystemLogDto dto) {
        if (dto == null) {
            return ServiceResultUtil.fail(RespCode.ERROR, "参数错误：请求入参不能为空");
        }
        log.info("getPurchaseLogPage method start with dto::{}", dto);
        Date[] time = dto.getLogTime();
        if (time != null && time.length > 0) {
            dto.setStartDate(time[0]);
            //结束日期为当天23点59分59秒
            Date end = time[1];
            Date date = DateFormatUtils.todayLastDate(end);
            dto.setEndDate(date);

        }
        //分页
        Integer pageIndex = 1;
        Integer pageSize = 10;
        if (dto.getPageIndex() != null && dto.getPageIndex() > 0) {
            pageIndex = dto.getPageIndex();
        }
        if (dto.getPageSize() != null && dto.getPageSize() > 0) {
            pageSize = dto.getPageSize();
        }
        dto.setPageIndex(pageIndex);
        dto.setPageSize(pageSize);
        return liveSystemLogService.getLiveSystemLog(dto);
    }


    @RequestMapping("getRedisCachePage")
    public ServiceResult<PagedListDTO<RedisCacheDto>> getRedisCachePage(@RequestBody RedisCacheDto redisCacheDto) {
        return ServiceResultUtil.success(redisService.getRedisCachePage(redisCacheDto));
    }

    /**
     * 根据Key删除缓存
     */
    @RequestMapping("deleteCache")
    public ServiceResult<Integer> deleteCache(@RequestBody RedisCacheDto redisCacheDto) {
        return ServiceResultUtil.success(redisService.deleteCache(redisCacheDto));
    }
    /**
     * 清空所有缓存
     */
    @RequestMapping("clearRedis")
    public ServiceResult<Integer> clearRedis(@RequestBody RedisCacheDto redisCacheDto) {
        return ServiceResultUtil.success(redisService.clearRedis(redisCacheDto));
    }

}
